package com.bop.dehong.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.widget.ImageView;

import com.bop.dehong.R;

public class LoadingAnimation {
	private ImageView imageView;
	private AnimationDrawable animationDrawable;
	private Dialog loadingDialog;
	private Activity mActivity;
	
	public LoadingAnimation(Activity activity) {
		mActivity = activity;
		loadingDialog = new Dialog(activity, R.style.MyDialogStyle);
		View view = View.inflate(activity, R.layout.animation_layout, null);
		imageView = (ImageView) view.findViewById(R.id.animationIV);
		loadingDialog.setContentView(view);
	}
	
	/**
	 * 创建并初始化动画
	 */
	@SuppressLint("NewApi")
	public void createAnimation() {	
		if (loadingDialog.isShowing()) {
			return;
		}
		animationDrawable = (AnimationDrawable) imageView.getDrawable();
		if (!animationDrawable.isRunning()) {
			animationDrawable.start();
		}
		if (mActivity.isFinishing()) {
			return;
		}
		loadingDialog.show();
	}
	
	/**
	 * 隐藏动画
	 */
	public void dismissAnimation() {
		if (loadingDialog != null) {
			animationDrawable = (AnimationDrawable) imageView.getDrawable();
			if (animationDrawable.isRunning()) {
				animationDrawable.stop();
			}
			loadingDialog.dismiss();
		}
	}
}
